//
//  RealRewardViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/25.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "RealRewardViewController.h"
#import "ExitProvinceViewController.h"
#import "ExitCityViewController.h"
@interface RealRewardViewController ()<UITextFieldDelegate>
{
    UIButton *_selectedBtn;
    NSString *_provinceCode;
    NSString *_CityCode;
}

@end

@implementation RealRewardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"彩蛋奖品邮寄地址";
    self.sureSubmitBtn.layer.cornerRadius=4;
    self.sureSubmitBtn.clipsToBounds=YES;
    self.isMan.selected=YES;
    _selectedBtn=self.isMan;
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

//选择男士
- (IBAction)isMan:(id)sender
{
    _selectedBtn.selected=NO;
    UIButton *btn=sender;
    btn.selected=YES;
    _selectedBtn=btn;
}
//选择女士
- (IBAction)isWoman:(id)sender
{
     _selectedBtn.selected=NO;
    UIButton *btn=sender;
    btn.selected=YES;
    _selectedBtn=btn;
}

//确认提交
- (IBAction)sureSubmit:(id)sender
{
    if (self.province.text.length==0) {
        [MBProgressHUD showError:@"所在省不能为空"];
    }else if (self.city.text.length==0) {
        [MBProgressHUD showError:@"所在城市不能为空"];
    }else if (self.detailAddress.text.length==0) {
        [MBProgressHUD showError:@"详细地址不能为空"];
        [self.detailAddress becomeFirstResponder];
    }else if (self.email.text.length==0) {
        [MBProgressHUD showError:@"邮政编码不能为空"];
        [self.email becomeFirstResponder];
    }else if (self.email.text.length>6) {
        [MBProgressHUD showError:@"请输入正确的邮政编码"];
        [self.email becomeFirstResponder];
    }else if (self.name.text.length==0) {
        [MBProgressHUD showError:@"姓名不能为空"];
        [self.name becomeFirstResponder];
    }else if (self.phoneNum.text.length==0) {
        [MBProgressHUD showError:@"手机号不能为空"];
        [self.phoneNum becomeFirstResponder];
    }else if ([GeneralTool valiMobile:self.phoneNum.text]!=nil)
    {
        [MBProgressHUD showError:[GeneralTool valiMobile:self.phoneNum.text]];
    }else
    {
    //http://192.168.1.107:3722/Activity/ColorEggsSubmitEntity
//    m_ColorEggsSubmitEntityPara(UserCode,GetNoteID,ProvinceCode,ProvinceName,CityCode,CityName,DAddress,PostCode,LinkMan,LinkMobile,Sex,Timestamp,Sign)
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSString *noteID=[NSString stringWithFormat:@"%ld",_noteID];
    NSString *str=nil;
    if (self.isMan.selected) {
        str=@"1";
    }else
    {
       str=@"2";
    }
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,
                        @"GetNoteID":noteID,
                        @"ProvinceCode":_provinceCode,
                        @"ProvinceName":self.province.text,
                        @"CityCode":_CityCode,
                        @"CityName":self.city.text,
                        @"DAddress":self.detailAddress.text,
                        @"PostCode":self.email.text,
                        @"LinkMan":self.name.text,
                        @"LinkMobile":self.phoneNum.text,
                        @"Sex":str,
                        @"Timestamp":timeStamp,
                        };
    NSString *sign=[GeneralTool GetSin:dic];
    NSDictionary *dataDic=@{@"UserCode":GETUSERCODE,
                            @"GetNoteID":noteID,
                            @"ProvinceCode":_provinceCode,
                            @"ProvinceName":self.province.text,
                            @"CityCode":_CityCode,
                            @"CityName":self.city.text,
                            @"DAddress":self.detailAddress.text,
                            @"PostCode":self.email.text,
                            @"LinkMan":self.name.text,
                            @"LinkMobile":self.phoneNum.text,
                            @"Sex":str,
                            @"Timestamp":timeStamp,
                            @"Sign":sign};
    NSDictionary *requestDic=@{@"m_ColorEggsSubmitEntityPara":dataDic};
    [RequestTool requestDataWithType:@"post" andUrlString:@"Activity/ColorEggsSubmitEntity" andParameters:requestDic andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showError:@"提交失败，请稍后再试"];
        }
        if ([responses[@"IsSuccess"]integerValue]==1) {
            [self.navigationController popViewControllerAnimated:YES];
        }else
        {
           [MBProgressHUD showSuccess:responses[@"ReturnObject"]];
        }
    }];
  }
}

- (IBAction)goToChooseCity:(id)sender
{
    ExitCityViewController *city=[[ExitCityViewController alloc]init];
    city.provinceCode=_provinceCode;
    city.passCityBlock=^(NSString *cityName,NSString *cityCode)
    {
        self.city.text=cityName;
        _CityCode=cityCode;
    };
    city.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:city animated:YES];
}

- (IBAction)goToChooseProvice:(id)sender {
    ExitProvinceViewController *province=[[ExitProvinceViewController alloc]init];
    province.hidesBottomBarWhenPushed=YES;
    province.passProvinceBlock=^(NSString *provinceName,NSString *provinceCode){
        self.province.text=provinceName;
        _provinceCode=provinceCode;
    };
    [self.navigationController pushViewController:province animated:YES];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
@end
